import type { Recordable } from '@vben/types';

import { requestSystemClient } from '#/api/request';

export namespace SystemUserApi {
  export interface SystemUser {
    [key: string]: any;
    userId: number;
    userName: string;
    userPassword?: string;
    userNickName: string;
    deptId?: null | number;
    deptName: string;
    postIds?: [];
    userEmail?: string;
    userPhone?: string;
    userSex?: 0 | 1 | 2; // 0=未知,1=男,2=女
    userAvatar?: string;
    userStatus: 0 | 1; // 0=停用,1=正常
    loginIp?: string;
    loginDate?: Date | null | string;
    remark?: null | string;
    creator?: string;
    createTime: Date | string;
    updater?: string;
    updateTime: Date | string;
    deleted: 0 | 1; // 0=未删除,1=已删除
    tenantId: number;

    // 树形/显示相关扩展字段（可选）
    label?: string; // 用于树形显示
    id?: number; // 兼容性字段
    children?: SystemUser[]; // 子级用户（如果有层级关系）
  }
}

/**
 * 获取用户列表数据
 */
async function getUserPage(params: Recordable<any>) {
  const data = requestSystemClient.get<Array<SystemUserApi.SystemUser>>(
    '/system/user/page',
    { params },
  );
  return data;
}

/**
 * 创建用户
 * @param data 用户数据
 */
async function createUser(
  data: Omit<SystemUserApi.SystemUser, 'children' | 'userId'>,
) {
  return requestSystemClient.post('/system/user/create', data);
}

/**
 * 更新用户
 * @param data 用户数据（需包含userId）
 */
async function updateUser(data: Omit<SystemUserApi.SystemUser, 'children'>) {
  return requestSystemClient.put('/system/user/update', data);
}

/**
 * 删除用户
 * @param userId 用户 ID
 */
async function deleteUser(userId: number) {
  return requestSystemClient.delete(`/system/user/delete/${userId}`);
}

export { createUser, deleteUser, getUserPage, updateUser };
